app = require("express")();
var bodyParser=require("body-parser");
var mysql = require("mysql");

//设置请求头信息
//ajax
app.use(bodyParser.json());
//form
app.use(bodyParser.urlencoded({extended:false}));


var connection = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"aladeng"

});
connection.connect();


//设置路由
app.post("/recommend",function (req, res) {
	var sql= 'SELECT * FROM recommend';
	connection.query(sql, function (error, results, fields) {
		var lunboArr = results[0].lunbo.split("@lunbo@");
		var xinpinArr = results[0].xinpin.split("@xinpin@");
		var jinri1 = results[0].jinri1.split("@big@");
		var jinri1small = jinri1[1].split("@small@");
		var arr1=[];
		for(var i=0;i<jinri1small.length;i++){
			let smallArr = jinri1small[i].split("@data@");
			var obj1={
				smallurl: smallArr[0],
				name: smallArr[1],
				price: smallArr[2],
				id: smallArr[3]
			}
			arr1.push(obj1);
		}
		var jinri2 = results[0].jinri2.split("@big@");
		var jinri2small = jinri2[1].split("@small@");
		var arr2=[];
		for(var i=0;i<jinri2small.length;i++){
			let smallArr = jinri2small[i].split("@data@");
			var obj2={
				smallurl: smallArr[0],
				name: smallArr[1],
				price: smallArr[2],
				id: smallArr[3]
			}
			arr2.push(obj2);
		}
		var allArr = {imgcircle:[{big:jinri1[0],small:arr1},{big:jinri2[0],small:arr2}],lunboArr:lunboArr,xinpinArr:xinpinArr}
		
		
        res.send({allArr:allArr})
       
    });	
   
});

	//贼惦记页面路由
	app.post("/zeidianji",function  (req,res) {
		var sql= 'SELECT * FROM zeidianji';
		connection.query(sql,function  (error, results, fields) {
			var tuijian = results[0].tuijian.split("@data@");
			var shangpin1= results[0].shangpin1.split("@shangpin@");
			console.log(shangpin1[0]);
			var arr1 =[];
			for (var i = 0; i < shangpin1.length; i++) {
				var single=shangpin1[i].split("@data@");
				console.log(single[0],single[1],single[2]);
				var obj1  = {
					urls:single[0],
					name:single[1],
					time:single[2]
				}
				console.log(obj1);
				arr1.push(obj1);
			}
			var arr2=[];
			var shangpin2= results[0].shangpin2.split("@shangpin@");
			for (var i = 0; i < shangpin2.length; i++) {
				var single=shangpin2[i].split("@data@");
				console.log(single[0],single[1],single[2]);
				var obj1  = {
					urls:single[0],
					name:single[1],
					time:single[2]
				}
				console.log(obj1);
				arr2.push(obj1);
			}
			console.log(arr2);
			res.send({tuijian:[{tu1:tuijian[0],tu2:tuijian[1],tu3:tuijian[2]}],shangpin1:arr1,shangpin2:arr2});
		});
	});
	//登录界面路由
	app.post("/login",function  (req,res) {
		var sql = 'SELECT * FROM userconfidence'
		connection.query(sql,function (error,results,fields) {
			console.log(req.body.useraccount);
			//比对用户信息
		})
		var  user = 'SELECT * FROM userconfidence WHERE useraccount=	'+req.body.useraccount+' AND password='+req.body.userpassword+'';
		connection.query(user,function  (err,results) {
			console.log(err);
			console.log(results);
			if (results.length==0) {
				res.send({errs:0,msg:"查询失败"});
			} else{
				res.send({errs:1,results:results});
			}
		})
	})
//查询用户订单
app.post("/selectuser",function  (req,res) {	
	var  sql  = 'SELECT * FROM '+req.body.ordername;
	connection.query(sql,function  (err,results) {
		console.log(err);
		console.log(results);
		if (results.length==0) {
			res.send({errs:0,msg:"查询失败"});
		} else{
			res.send({errs:1,results:results});
		}
	});
})
//用户注册路由
app.post("/registered",function  (req,res) {
	var  user = 'SELECT * FROM userconfidence WHERE useraccount=	'+req.body.useraccount+'';
		connection.query(user,function  (err,results) {//查询
			console.log(results);
			if (results.length==0) {//没有查到语序添加
				res.send({errs:1,results:"没有查到语序添加"});
				var  sql  = 'INSERT INTO userconfidence (useraccount,password) VALUES ('+req.body.useraccount+','+req.body.setpassword1+')' ;
				connection.query(sql,function  (err,results) {
					
					if (results.length==0) {
						res.send({errs:0,msg:"添加失败"});
					} else{//添加成功后进行查询
						console.log("插入成功");
						var  user = 'SELECT * FROM userconfidence WHERE useraccount=	'+req.body.useraccount+'';
						connection.query(user,function  (err,results) {
							console.log(results);
							if (results.length==0) {
								res.send({errs:0,msg:"查询失败"});
							} else{//查询成功后 进行修改
								console.log("查询成功后 进行修改");
								console.log(results[0].id);
								var  user = 'UPDATE  userconfidence SET ordername="order0002",username="用户'+results[0].id+'", userheader="../../../../static/G-img/user.jpg" , shoppingCarName="shoppingCar0001"WHERE useraccount = '+req.body.useraccount;
								connection.query(user,function  (err,results) {
									console.log(err);
									
								})
								
							}
						})
					}
				});
				
			} else{
				res.send({errs:0,msg:"您输入的手机号已被占用"});
			}
		})

})
//全部订单路由
app.post("/lookorder",function  (req,res) {
	var sql = 'SELECT * FROM commodity  WHERE id=	'+req.body.id+'';
	connection.query(sql,function  (error,results) {
		console.log(results);
		if (results.length==0) {
			res.send({err:0,msg:"数据查询失败"});
		} else{
			res.send({err:1,results:results});
		}
	})
})


//美食
app.post("/foodzone",function(req,res){
	var sql = 'SELECT * FROM food';
	connection.query(sql,function(error,results,fields){
		var shuffing = results[0].shuffling.split("@shuffing@");
		var cakesCategory = results[0].cakesCategory.split("@title@");
		var meishiArr1 = [];
		for(var i = 0; i < cakesCategory.length;i++){
			let listArr = cakesCategory[i].split("@footlist@");
			var meishi={
				img:listArr[0],
				name:listArr[1]
			}
			meishiArr1.push(meishi);
		}
		var Detonation = results[0].Detonation.split("@list@");
		var yinliaoArr2= [];
		for (var i = 0; i < Detonation.length;i++){
			let drin = Detonation[i].split("@data@");
			var drinks={
				img:drin[0],
				name:drin[1],
				maney:drin[2]
			}
			yinliaoArr2.push(drinks);
		}
		
		var foodArr = {img:shuffing,aaa:meishiArr1,drinks:yinliaoArr2}
		res.send({foodArr:foodArr})
	})
	
});

//分类
app.post("/class",function(req,res){
	var sql = "SELECT * FROM classification";
	connection.query(sql,function(error,results,fields){
		var list = results[0].list.split("@list@");
		var postal1 = results[0].Postal.split("@data@");
		var postalArr1 = [];
		for(var i = 0;i < postal1.length;i++){
			let Direct1 = postal1[i].split("@img@");
			var Directlist1={
				img:Direct1[0],
				name:Direct1[1]
			}
			postalArr1.push(Directlist1);
		}
		var product2 = results[0].product.split("@data@");
		var productArr2 = [];
		for(var i = 0;i < product2.length;i++){
			let Direct2 = product2[i].split("@img@");
			var Directlist2={
				img:Direct2[0],
				name:Direct2[1]
			}
			productArr2.push(Directlist2);
		}
		var Facial3 = results[0].Facial.split("@data@");
		var Facial3Arr = [];
		for(var i = 0;i < Facial3.length;i++){
			let Fac = Facial3[i].split("@img@");
			var Directlist3={
				img:Fac[0],
				name:Fac[1]
			}
			Facial3Arr.push(Directlist3);
		}
		var classArr = {list:list,postal:postalArr1,product:productArr2,Facial:Facial3Arr}
		
		res.send({classArr:classArr})
	})
})

//搜索
app.post("/search",function(req,res){
	var sign = req.body.sign;
	var countries=[];
	if(req.body.countries!=undefined){
		countries=req.body.countries;
	}
	var Commodit=[];
	if(req.body.Commodit!=undefined){
		Commodit=req.body.Commodit;
	}
	var classifi=[];
	if(req.body.classifi!=undefined){
		classifi=req.body.classifi;
	}
	
	switch (sign){
		case '默认':
			var sql = "SELECT * FROM commodity";
			connection.query(sql,function(error,results,fields){
				var arr = [];
				for(var i = 0;i < results.length;i++){
					let obj={id:results[i].id,imgurl:results[i].imgurl,title:results[i].goodsname,price:results[i].price,Brand:results[i].brandintro,nationalflag:results[i].nationalflag,areaType:results[i].areaType}
					arr.push(obj)
					
				}
				res.send({searchpage:arr});
			})		
			break;
		case "销量":
//		ORDER BY sales DESC
			var sql = "SELECT * FROM commodity ";
			if(countries!=''||Commodit!=''||classifi.length!=0){
				sql+="WHERE ";
			}
			if(countries!=''){
				if(classifi.length!=0){
					sql+="areaType = '"+countries+"' and ";
				}else if(classifi.length==0&&Commodit==''){
					sql+="areaType = '"+countries+"' ";
				}
				
			}
			if(Commodit!=''){
				if(classifi.length!=0){
					sql+="goodstype = '"+Commodit+"' and ";
				}else{
					sql+="goodstype = '"+Commodit+"' ";
				}
				
			}
			if(classifi.length!=0){
				for(var i=0;i<classifi.length;i++){
					if(i == classifi.length-1){
						sql+="classification='"+classifi[i]+"' "
					}else{
						sql+="classification='"+classifi[i]+"' or "
					}
					
				}
			}
			sql+="ORDER BY sales DESC";
			connection.query(sql,function(error,results,fields){
				var salesArr = [];
				for(var i = 0;i < results.length;i++){
					let salesobj = {id:results[i].id,imgurl:results[i].imgurl,title:results[i].goodsname,price:results[i].price,Brand:results[i].brandintro,nationalflag:results[i].nationalflag,areaType:results[i].areaType,}
					salesArr.push(salesobj)
				}
				res.send({ascending:salesArr})
			})
			break;
		case "最新":
			var sql = "SELECT * FROM commodity ";
			if(countries!=''||Commodit!=''||classifi.length!=0){
				sql+="WHERE ";
			}
			if(countries!=''){
				if(classifi.length!=0){
					sql+="areaType = '"+countries+"' and ";
				}else if(classifi.length==0&&Commodit==''){
					sql+="areaType = '"+countries+"' ";
				}
				
			}
			if(Commodit!=''){
				if(classifi.length!=0){
					sql+="goodstype = '"+Commodit+"' and ";
				}else{
					sql+="goodstype = '"+Commodit+"' ";
				}
				
			}
			if(classifi.length!=0){
				for(var i=0;i<classifi.length;i++){
					if(i == classifi.length-1){
						sql+="classification='"+classifi[i]+"' "
					}else{
						sql+="classification='"+classifi[i]+"' or "
					}
					
				}
			}
			sql+="ORDER BY id DESC";
			connection.query(sql,function(error,results,fields){
				console.log(error)
				var idArr = [];
				for(var i = 0;i < results.length;i++){
					let idobj = {id:results[i].id,imgurl:results[i].imgurl,title:results[i].goodsname,price:results[i].price,Brand:results[i].brandintro,nationalflag:results[i].nationalflag,areaType:results[i].areaType,}
					idArr.push(idobj);
				}
				res.send({id:idArr})
			})
			break;
		case "价格1":
			var sql = "SELECT * FROM commodity ";
			if(countries!=''||Commodit!=''||classifi.length!=0){
				sql+="WHERE ";
			}
			if(countries!=''){
				if(classifi.length!=0){
					sql+="areaType = '"+countries+"' and ";
				}else if(classifi.length==0&&Commodit==''){
					sql+="areaType = '"+countries+"' ";
				}
				
			}
			if(Commodit!=''){
				if(classifi.length!=0){
					sql+="goodstype='"+Commodit+"' and ";
				}else{
					sql+="goodstype='"+Commodit+"' ";
				}
				
			}
			if(classifi.length!=0){
				for(var i=0;i<classifi.length;i++){
					if(i == classifi.length-1){
						sql+="classification='"+classifi[i]+"' "
					}else{
						sql+="classification='"+classifi[i]+"' or "
					}
					
				}
			}
			sql+="ORDER BY price DESC";
//			var sql = "SELECT * FROM commodity";
			connection.query(sql,function(error,results,fields){
				console.log(error,11111);
				var latestArr = [];
				for(var i = 0; i < results.length;i++){
					let lateobj = {id:results[i].id,imgurl:results[i].imgurl,title:results[i].goodsname,price:results[i].price,Brand:results[i].brandintro,nationalflag:results[i].nationalflag,areaType:results[i].areaType,}
						latestArr.push(lateobj);
					}
					res.send({latest:latestArr});
				
			})
			break;
		case "价格2":
			var sql = "SELECT * FROM commodity ";
			if(countries!=''||Commodit!=''||classifi.length!=0){
				sql+="WHERE ";
			}
			if(countries!=''){
				if(classifi.length!=0){
					sql+="areaType = '"+countries+"' and ";
				}else if(classifi.length==0&&Commodit==''){
					sql+="areaType = '"+countries+"' ";
				}
				
			}
			if(Commodit!=''){
				if(classifi.length!=0){
					sql+="goodstype = '"+Commodit+"' and ";
				}else{
					sql+="goodstype = '"+Commodit+"' ";
				}
				
			}
			if(classifi.length!=0){
				for(var i=0;i<classifi.length;i++){
					if(i == classifi.length-1){
						sql+="classification='"+classifi[i]+"' "
					}else{
						sql+="classification='"+classifi[i]+"' or "
					}
					
				}
			}
			sql+="ORDER BY price ASC";
			connection.query(sql,function(error,results,fields){
				console.log(sql);
				var latestArr = [];
				for(var i = 0; i < results.length;i++){
					let lateobj = {id:results[i].id,imgurl:results[i].imgurl,title:results[i].goodsname,price:results[i].price,Brand:results[i].brandintro,nationalflag:results[i].nationalflag,areaType:results[i].areaType,}
						latestArr.push(lateobj);
					}
					res.send({latest:latestArr});
				
			})
			break;
		default:
			break;
	}
	
	
	
})







//设置路由母婴页面
app.post("/motherson",function(req,res){
	var sql= 'SELECT * FROM motherson';
	connection.query(sql, function (error, results, fields){
		console.log(results);
		var lunboArr = results[0].lunbo.split("@lunbo@");
		var title1 = results[0].title1;
		var title2 = results[0].title2;
		var title3 = results[0].title3;
		//猜你喜欢
		var guessArr = results[0].guess.split("@data@");
		//奶粉专区
		//以@big@为标识切割 naiufen 字符串 naifen[0] 为大图  naifen[1]为小图
		var naifen = results[0].naifen.split("@big@");
		//以@small@为标识切割 naiufen[1] 字符串  分为三个部分 每个部分是小图里的内容
		var naifensmall = naifen[1].split("@small@");
		var naifenArr=[];
		for(var i = 0;i < naifensmall.length; i++){
			let smallArr = naifensmall[i].split("@data@");
			var  smallobj1 = {
				smallurl: smallArr[0],
				name:smallArr[1],
				price:smallArr[2],
				id:smallArr[3]
			}
			naifenArr.push(smallobj1);
		}
		//纸尿裤专区
		var zhiniaoku = results[0].zhiniaoku.split("@big@");
		var zhiniaokusmall = zhiniaoku[1].split("@small@");
		var zhiniaokuArr=[];
		for(var i = 0;i < zhiniaokusmall.length; i++){
			let smallArr = zhiniaokusmall[i].split("@data@");
			var  smallobj2 = {
				smallurl: smallArr[0],
				name:smallArr[1],
				price:smallArr[2],
				id:smallArr[3]
			}
			zhiniaokuArr.push(smallobj2);
		}

		var allArr = {
			lunboArr:lunboArr,
			imgcircle:[{
				title:title1,
				big:naifen[0],
				small:naifenArr
			},{
				title:title2,
				big:zhiniaoku[0],
				small:zhiniaokuArr
			}],
			guesslove:{
				title:title3,
				lefturl:guessArr[0],
				leftname:guessArr[1],
				leftprice:guessArr[2],
				righturl:guessArr[3],
				rightname:guessArr[4],
				rightprice:guessArr[5]
			}
		}	
		res.send({allArr:allArr})
	});
});


//设置断货王页面路由
app.post("/goodsking",function(req,res){
	var sql= 'SELECT * FROM goodsking';
	connection.query(sql, function (error, results, fields) {
		console.log(results);
		var goods1 = results[0].goods1.split("@big@");
		var goods1small = goods1[1].split("@small@");
		var goodsArr1=[];
		for(var i=0;i<goods1small.length;i++){
			let smallArr = goods1small[i].split("@data@");
			var obj1={
				smallurl: smallArr[0],
				name: smallArr[1],
				price: smallArr[2],
				id: smallArr[3]
			}
			goodsArr1.push(obj1);
		}
		
		var allArr = {
			imgcircle:[{
				big:goods1[0],
				small:goodsArr1
			},{
				big:goods1[0],
				small:goodsArr1
			},{
				big:goods1[0],
				small:goodsArr1
			}]
		}
		res.send({allArr:allArr})
	});
});



//设置商品详情页路由
app.post("/goodsdetails",function(req,res){
	var id = req.body.id;
	var sql = 'SELECT * FROM commodity where id='+id;
	connection.query(sql, function (error, results, fields){
//		console.log(results)
		
		var lunboArr = results[0].lunbo.split("@data@");
		var id = results[0].id;
		var buyingprice = results[0].buyingprice;
		var nationalflag = results[0].nationalflag;
		var areaType = results[0].areaType;
		var goodsname = results[0].goodsname;
		var price = results[0].price;
		var postage = results[0].postage;
		var imgurl = results[0].imgurl;
		var goodsintro = results[0].goodsintro;
		var brandname = results[0].brandname;
		var brandintro = results[0].brandintro;
		var taxes = results[0].taxes;
		var aaa= "SELECT * FROM commodity where brandname = '"+brandname+"'";
		connection.query(aaa, function (err, results, fields){
//			console.log(results);
			var brandArr = [];
			for(var i=0; i<results.length;i++){
				var obj = {
					 imgurl : results[i].imgurl,
					 goodsname : results[i].goodsname,
					 goodsprice :  results[i].price,
					 goodsid : results[i].id
				};
				brandArr.push(obj);
			}
//			console.log(brandArr)

			var num =  brandArr.length;
			console.log(num);
			var midbrandArr = [];
			for(var j=0; j<brandArr.length;j++){
				if(brandArr[j].goodsid != id){
					midbrandArr.push(brandArr[j]);
				}
			}
			if(midbrandArr.length > 3){
				var newbrandArr = midbrandArr.slice(1,4);
			}else{
				var newbrandArr = midbrandArr.slice(0,3);
			}
			
			console.log(newbrandArr)
			
			var allArr = {lunboArr:lunboArr,nationalflag:nationalflag,buyingprice:buyingprice,areaType:areaType,price:price,goodsname:goodsname,postage:postage,imgurl:imgurl,goodsintro:goodsintro,brandname:brandname,brandintro:brandintro,newbrandArr:newbrandArr,id:id,taxes:taxes,num:num}
		res.send({allArr:allArr});
			
			
		});	
	});
});


//品牌名字页面
app.post("/goodsname",function(req,res){
	console.log(req);
	var brand = req.body.brand;
	console.log(brand);
	
	var sql = "SELECT * FROM commodity where brandname = '"+brand+"'";
	
	connection.query(sql, function (err, results, fields){
		console.log(results);
		var  brandname = results[0].brandname;
		var  brandintro = results[0].brandintro;
		var  areaType = results[0].areaType;
		var brandArr = [];
		for(var i = 0;i < results.length; i++){
			var obj = {
				imgurl: results[i].imgurl,
				goodsname : results[i].goodsname,
				goodsintro: results[i].goodsintro,
				nationalflag: results[i].nationalflag,
				price:  results[i].price,
				id : results[i].id
			}
			brandArr.push(obj);
		}
		
		console.log(brandArr);
		var allArr = {brandname:brandname,brandintro:brandintro,areaType:areaType,brandArr:brandArr}
		res.send({allArr:allArr});
	});
	
	
	
	
	
});





//购物车页面
app.post("/ShoppingCart",function (req, res) {
	var myCar=req.body.myCar;
	var lose = [];
	var noLose = [];
	var Japan={commoditys:[],areaType:"日本直邮",allPrice:0,allTaxes:0,endPrice:0}
	var Korea={commoditys:[],areaType:"韩国直邮",allPrice:0,allTaxes:0,endPrice:0}
	var England={commoditys:[],areaType:"英国直邮",allPrice:0,allTaxes:0,endPrice:0}
	var China={commoditys:[],areaType:"中国直邮",allPrice:0,allTaxes:0,endPrice:0}
	var America={commoditys:[],areaType:"美国直邮",allPrice:0,allTaxes:0,endPrice:0}
	var NewZealand={commoditys:[],areaType:"新西兰直邮",allPrice:0,allTaxes:0,endPrice:0}
	var over = function(){
		if(Japan.commoditys.length!=0){
			noLose.push(Japan);
		}
		if(Korea.commoditys.length!=0){
			noLose.push(Korea);
		}
		if(England.commoditys.length!=0){
			noLose.push(England);
		}
		if(China.commoditys.length!=0){
			noLose.push(China);
		}
		if(America.commoditys.length!=0){
			noLose.push(America);
		}
		if(NewZealand.commoditys.length!=0){
			noLose.push(NewZealand);
		}
		res.send({noLose:noLose,lose:lose})
	}
	for(let i=0;i<myCar.length;i++){
		let sql= 'SELECT * FROM commodity WHERE id='+myCar[i].id;
		
		connection.query(sql, function (error, results, fields) {
			
			if(results[0].isLose==0){

				if(results[0].areaType=='日本'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					Japan.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						Japan.allPrice+=results[0].price*myCar[i].num;
						Japan.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						Japan.endPrice=Japan.allPrice-Japan.allTaxes;
					}
					
				}else if(results[0].areaType=='韩国'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					Korea.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						Korea.allPrice+=results[0].price*myCar[i].num;
						Korea.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						Korea.endPrice=Korea.allPrice-Korea.allTaxes;
					}
				}else if(results[0].areaType=='英国'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					England.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						England.allPrice+=results[0].price*myCar[i].num;
						England.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						England.endPrice=England.allPrice-England.allTaxes;
					}
				}else if(results[0].areaType=='中国'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					China.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						China.allPrice+=results[0].price*myCar[i].num;
						China.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						China.endPrice=China.allPrice-China.allTaxes;
					}
				}else if(results[0].areaType=='美国'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					America.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						America.allPrice+=results[0].price*myCar[i].num;
						America.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						America.endPrice=America.allPrice-America.allTaxes;
					}
				}else if(results[0].areaType=='新西兰'){
					results[0].xuanzhong = myCar[i].xuanzhong;
					results[0].num = myCar[i].num;
					NewZealand.commoditys.push(results[0]);
					if(myCar[i].xuanzhong==1){
						NewZealand.allPrice+=results[0].price*myCar[i].num;
						NewZealand.allTaxes+=results[0].taxes*0.01*results[0].price*myCar[i].num;
						NewZealand.endPrice=NewZealand.allPrice-NewZealand.allTaxes;
					}
				}
			}else{
				results[0].num = myCar[i].num;
				lose.push(results[0]);
			}
			if(i==myCar.length-1){
				over();
			}
		});
		
	}
   
});

//获取购物车表数据
app.post("/getmyCar",function(req,res){
	var shoppingCarName = req.body.shoppingCarName;
	var sql = 'SELECT * FROM '+shoppingCarName;
	connection.query(sql, function (error, results, fields){
		res.send(results)	
	});
});

//更改购物车数据
app.post("/changeCar",function(req,res){
	var sign = req.body.sign;
	switch (sign){
		case "更改数量":
			var id = req.body.theChange.id;
			var num = req.body.theChange.num;
			var shoppingCarName=req.body.shoppingCarName;
			var sql = 'update '+shoppingCarName+' set num = '+num+' where id = '+id;
			connection.query(sql, function (error, results, fields){	
				var sql2 = 'SELECT * FROM '+shoppingCarName;
				connection.query(sql2, function (error, results, fields){
					res.send(results)	
				});
			});
			break;
		case "更改选中":
			var id = req.body.theChange.id;
			var xuanzhong = req.body.xuanzhong;
			var shoppingCarName=req.body.shoppingCarName;
			var sql = 'update '+shoppingCarName+' set xuanzhong = '+xuanzhong+' where id = '+id+' limit 1';
			connection.query(sql, function (error, results, fields){	
				var sql2 = 'SELECT * FROM '+shoppingCarName;
				connection.query(sql2, function (error, results, fields){
					res.send(results)	
				});
			});
			break;
		case "更改全选":
			var theChange = req.body.theChange;
			var xuanzhong = req.body.xuanzhong;
			var shoppingCarName=req.body.shoppingCarName;
			var sql = 'update '+shoppingCarName+' set xuanzhong = '+xuanzhong+' where ';
			for(var i=0;i<theChange.length;i++){
				if(i==theChange.length-1){
					sql+='id='+theChange[i].id;
				}else{
					sql+='id='+theChange[i].id+' or ';
				}				
			}	
			connection.query(sql, function (error, results, fields){	
				var sql2 = 'SELECT * FROM '+shoppingCarName;
				connection.query(sql2, function (error, results, fields){
					res.send(results)	
				});
			});
			break;
		case "添加数据":
			var id = req.body.id;
			console.log(id)
			var shoppingCarName=req.body.shoppingCarName;
			var sql = 'insert into '+shoppingCarName+'(id,num) values ('+id+','+1+')';
			connection.query(sql, function (error, results, fields){	
				console.log(error)
				var sql2 = 'SELECT * FROM '+shoppingCarName;
				connection.query(sql2, function (error, results, fields){
					res.send(results)	
				});
			});
			break;
		case "删除数据":
			var theChange = req.body.theChange;
			var data = req.body.data;
			var shoppingCarName=req.body.shoppingCarName;

			var sql = 'delete from '+shoppingCarName+' where ';
			for(var i=0;i<data.commoditys.length;i++){
				if(data.commoditys[i].xuanzhong!=0){
					if(i==data.commoditys.length-1){
						sql+='id='+data.commoditys[i].id;
					}else{
						sql+='id='+data.commoditys[i].id+' or ';
					}
				}
								
			}	
			connection.query(sql, function (error, results, fields){	
				var sql2 = 'SELECT * FROM '+shoppingCarName;
				connection.query(sql2, function (error, results, fields){
					res.send(results)	
				});
			});
			break;
	}

});

//更改订单数据
app.post("/changedaifu",function(req,res){
	var ordername = req.body.ordername;
	var jieObj = req.body.jieObj;
	var sql = 'SELECT * FROM '+ordername;
	connection.query(sql, function (error, results, fields){
		res.send(results)	
	});
});



//静态文件
app.get("*",function (req, res) {
    res.sendFile(__dirname+req.url);
});

app.listen(8000,function(){
	console.log("服务器启动成功")
});